const express = require('express')
const expressStatic = require('express-static')
const bodyParser = require('body-parser')
const multer = require('multer')
const multerObj = multer({ dest: './static/upload' })
const mysql = require('mysql')
const cookieParser = require('cookie-parser')
const cookieSession = require('cookie-session')
const consolidate = require('consolidate')
const expresRoute = require('express-route')

let server = express()
server.listen(8080)

// 1、获取请求数据
server.use(bodyParser.urlencoded({ extended: false }))
server.use(multerObj.any())

// 2、cookie, session
server.use(cookieParser());
(function () {
  let keys = []
  for (let i = 0; i < 100000; i++) {
    keys.push('a_' + Math.random())
  }
  server.use(cookieSession({
    name: 'admin_id',
    keys,
    maxAge: 20 * 60 * 1000  // 20min
  }))
})()

// 3、模板
server.engine('html', consolidate.ejs)
server.set('views', 'template')
server.set('view engine', 'html')

// 4、router
server.use('/', require('./router/web')())
server.use('/admin', require('./router/admin/index')()) // index可以不引，栗require('./router/admin')

// 5、default, static
server.use(expressStatic('./static/'))
